﻿namespace FiftyOne.Foundation.Mobile.Detection.Wurfl.Configuration
{
    using System;
    using System.Configuration;
    using System.Reflection;

    public sealed class CapabilityCollection : ConfigurationElementCollection
    {
        public void Add(CapabilityElement capability)
        {
            if (capability == null)
            {
                throw new ArgumentNullException("capability");
            }
            this.BaseAdd(capability);
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            base.BaseAdd(element, false);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new CapabilityElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CapabilityElement) element).CapabilityName;
        }

        public int IndexOf(CapabilityElement capability)
        {
            if (capability == null)
            {
                throw new ArgumentNullException("capability");
            }
            return base.BaseIndexOf(capability);
        }

        public void Remove(CapabilityElement capability)
        {
            if (capability == null)
            {
                throw new ArgumentNullException("capability");
            }
            if (base.BaseIndexOf(capability) >= 0)
            {
                base.BaseRemove(capability.CapabilityName);
            }
        }

        public void Remove(string capabilityName)
        {
            if (string.IsNullOrEmpty(capabilityName))
            {
                throw new ArgumentNullException("capabilityName");
            }
            base.BaseRemove(capabilityName);
        }

        public void RemoveAt(int index)
        {
            if ((index < 0) || (index > (base.Count - 1)))
            {
                throw new ArgumentOutOfRangeException("index");
            }
            base.BaseRemoveAt(index);
        }

        public CapabilityElement this[string capabilityName]
        {
            get
            {
                return (CapabilityElement) base.BaseGet(capabilityName);
            }
        }

        public CapabilityElement this[int index]
        {
            get
            {
                return (CapabilityElement) base.BaseGet(index);
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
    }
}

